﻿Imports System.Text
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Namespace Controls
    Public Class RichTextIRC
        Inherits System.Windows.Forms.RichTextBox
        Public Const IrcColorForm As String = "\cC"
        Public Const IrcForeColorForm As String = "\cC([0-9][0-9]|[0-9])"
        Public Const IrcBackColorForm As String = "\cC([0-9][0-9]|[0-9]),([0-9][0-9]|[0-9])"
        Public Const IrcBoldForm As String = "\cB"
        Public Const IrcUnderLineForm As String = "\c_"
        Public Const IrcPlainForm As String = "\cO"
        Public Const IrcAllColorCodes As String = IrcPlainForm + "|" + IrcUnderLineForm + "|" + IrcBoldForm + "|" + IrcBackColorForm + "|" + IrcForeColorForm + "|" + IrcColorForm
        Public Const AllSymbols As String = "\\|\{|\}"
        Public Const EndOfText As String = "\par}"
        Public I, J, K As Integer
        Public len As Integer = 0
        Dim allText As ArrayList = New Essential.QutoaArray(170)
        Dim EAScroll As Boolean
        Dim colorTable As String = "{\colortbl ;"
        Dim rtfHeaderVar As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0" & MyBase.Font.FontFamily.Name & ";}}"
        Dim rtfTXTStart As String = "\viewkind4\uc1\pard\f0\fs" & Math.Round(MyBase.Font.Size * 2) & "\cb0\cf0"
        ReadOnly SB_BOTTOM As New IntPtr(7)
        ReadOnly SB_LINEDOWN As New IntPtr(1)
        ReadOnly SB_ENDSCROLL As New IntPtr(8)
        Const WM_VSCROLL As Integer = 277
        Const EM_SCROLLCARET As Integer = 56
        Dim colRegEx As Regex
        Declare Function ShellExecuteA Lib "shell32.dll" (ByVal hWnd As IntPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Integer) As IntPtr
        Public Sub New()
            MyBase.Multiline = True
            MyBase.DetectUrls = True
            SetupRTIRCBox()
        End Sub
        Private Sub SetupRTIRCBox()
            colorTable += "\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue127;\red0\green147\blue0;\red255\green0\blue0;\red127\green0\blue0;\red156\green0\blue156;\red252\green127\blue0;\red182\green255\blue0;\red0\green252\blue0;\red0\green147\blue147;\red0\green255\blue255;\red0\green0\blue252;\red255\green0\blue255;\red127\green127\blue127;\red210\green210\blue210;}"
            MyBase.Rtf = rtfHeader & colorTable
            colRegEx = New Regex(IrcAllColorCodes, RegexOptions.Compiled)
        End Sub
        Public Sub AddText(ByVal Text As String)
            Dim ircstringbuild As StringBuilder = New StringBuilder
            ircstringbuild.Append(Text)
            Dim final As String
            Dim rtproduct As StringBuilder = RenderText(ircstringbuild)
            If Not IsNothing(ircstringbuild) And Not IsNothing(rtproduct) Then final = rtproduct.ToString
            allText.Add(final & "\par")
            MyBase.Rtf = rtfHeader() & ReturnAllText() & EndOfText
        End Sub
        Private Function ReturnAllText() As String
            Dim atStrBuild As New StringBuilder
            For I = 0 To allText.Count - 1
                atStrBuild.Append(allText(I) & "\cf0\highlight0")
            Next
            Return atStrBuild.ToString
        End Function
        Private Function rtfHeader() As String
            Return rtfHeaderVar & rtfTXTStart & colorTable
        End Function
        Public Sub Notice(ByVal Message As String)
            AddText(CChar(ChrW(3).ToString()) & "5" & Message)
        End Sub
        Public Sub ChannelMessage(ByVal Message As String, Optional ByVal ColorNums As Integer() = Nothing)
            AddText(CChar(ChrW(3).ToString()) & "3" & Message)
        End Sub
        Public Sub IRCQuit(ByVal Message As String)
            AddText(CChar(ChrW(3).ToString()) & "2" & Message)
        End Sub
        Public Sub Kicked(ByVal Message As String)
            Try
                AddText(CChar(ChrW(3).ToString()) & "4" & Message)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
        Public Property EnableAutoScroll() As Boolean
            Get
                Return EAScroll
            End Get
            Set(ByVal value As Boolean)
                EAScroll = value
            End Set
        End Property
        Private Sub ScrollToBottom()
            Try
                'If EnableAutoScroll = True Then
                Dim msg As System.Windows.Forms.Message = Message.Create(Me.Handle, EM_SCROLLCARET, IntPtr.Zero, IntPtr.Zero)
                Me.DefWndProc(msg)
                msg = Message.Create(Me.Handle, WM_VSCROLL, SB_LINEDOWN, IntPtr.Zero)
                Me.DefWndProc(msg)
                msg = Message.Create(Me.Handle, WM_VSCROLL, SB_BOTTOM, IntPtr.Zero)
                Me.DefWndProc(msg)
            Catch Ex As Exception
            End Try
        End Sub
        Private Sub RichTextIRC_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles Me.LinkClicked
            ShellExecuteA(Me.Handle, "open", e.LinkText, "", "", 4)
        End Sub
        Private Sub RichTextIRC_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
            ScrollToBottom()
        End Sub
        Public Function RenderText(ByRef Text As StringBuilder) As StringBuilder
            Try
                Dim mat As Match = colRegEx.Match(Text.ToString)
                Dim A As String
                Dim B As String()
                Dim colfnum, colbgnum As Integer
                While mat.Success
                    I = mat.Index : J = mat.Length
                    'If Not mat.Index = Text.Length Then
                    len = Text.Length
                    Text.Remove(mat.Index, mat.Length)
                    'End If
                    If (Regex.Match(mat.Value, IrcBoldForm)).Success Then
                    End If
                    If (Regex.Match(mat.Value, IrcBackColorForm)).Success Then
                        A = mat.Value
                        A = A.Replace(CChar(ChrW(3).ToString()), "")
                        B = A.Split(",")
                        colfnum = Convert.ToDouble(B(0)) + 1
                        colbgnum = Convert.ToDouble(B(1)) + 1
                        Text.Insert(mat.Index, "\cb0\cf" & colfnum.ToString & "\highlight" & colbgnum.ToString)
                        'mat = Me.colRegEx.Match(Text.ToString(), Text.Length - len)
                    End If
                    If (Regex.Match(mat.Value, IrcForeColorForm)).Success Then
                        A = mat.Value
                        A = A.Replace(CChar(ChrW(3).ToString()), "")
                        colfnum = Convert.ToDouble(A) + 1
                        Text.Insert(mat.Index, "\cb0\cf" & colfnum.ToString & " ")
                    End If
                    If Not len > Text.Length Then mat = Me.colRegEx.Match(Text.ToString(), Text.Length - len)
                End While
                Return Text
            Catch ex As Exception
                MsgBox(ex.ToString & vbNewLine & vbNewLine & "Length: " & len & " " & "SB Length: " & Text.Length)
            End Try
        End Function
    End Class
End Namespace